package com.gzx.spring.apis.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.apis.result.RiskResult;
import com.gzx.risk.core.apis.result.xinyisou.XinyisouScore95;
import com.gzx.risk.core.apis.result.xinyisou.XinyisouScore95s;
import com.gzx.spring.apis.utils.XinyisouUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class XinyisouService {
	@Value("${apis.xinyisou.score95.appId}")
	private String score95AppId;
	@Value("${apis.xinyisou.score95.token}")
	private String score95Token;
	@Value("${apis.xinyisou.score95.apiUrl}")
	private String score95ApiUrl;

	@Value("${apis.xinyisou.score95s.appId}")
	private String score95sAppId;
	@Value("${apis.xinyisou.score95s.token}")
	private String score95sToken;
	@Value("${apis.xinyisou.score95s.apiUrl}")
	private String score95sApiUrl;

	@Value("${apis.xinyisou.env}")
	private String env;

	private final ObjectMapper objectMapper;

	public XinyisouService(ObjectMapper objectMapper) {
		this.objectMapper = objectMapper;
	}

	public String score95(String name, String id, String phone) throws Exception {
        if ("debug".equals(env)) {
            return ApiExec.loadJson("/debug/xinyisou/score95");
        }
        XinyisouUtil util = new XinyisouUtil().appId(score95AppId).token(score95Token).apiUrl(score95ApiUrl);
		return util.mirror_score95(env, name, id, phone);
	}

	public String score95s(String name, String id, String phone) throws Exception {
        if ("debug".equals(env)) {
            return ApiExec.loadJson("/debug/xinyisou/score95s");
        }
		XinyisouUtil util = new XinyisouUtil().appId(score95sAppId).token(score95sToken).apiUrl(score95sApiUrl);
		return util.puyu_score95s(env, name, id, phone);
	}

	public RiskResult score95(String groupName, ApiContext ctx) throws Exception {
		String apiName = "xinyisou_score95";
        String	json = this.score95(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(), ctx.getUserInfo().getPhone());
        log.debug("{}:{}", apiName, json);

        XinyisouScore95 obj = objectMapper.readValue(json, XinyisouScore95.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}

	public RiskResult score95s(String groupName, ApiContext ctx) throws Exception {
		String apiName = "xinyisou_score95s";
		String json = this.score95s(ctx.getUserInfo().getName(), ctx.getUserInfo().getId(), ctx.getUserInfo().getPhone());
        log.debug("{}:{}", apiName, json);

		XinyisouScore95s obj = objectMapper.readValue(json, XinyisouScore95s.class);
		obj.setGroupName(groupName);
		obj.setDataName(apiName);
		if (obj.getCode() == 0) {
			ctx.add(obj);
		} else {
			log.warn("{},code:{},msg:{}", apiName, obj.getCode(), obj.getMsg());
		}
		return obj;
	}
}
